<?php

require_once 'Tavs/Mail/Loader/Abstract.php';

class Tavs_Mail_Loader_File extends Tavs_Mail_Loader_Abstract
{
	/**
	 * Render Objectt
	 *
	 * @var Zend_View_Interface
	 */
	protected $_view;

	/**
	 * Script to be load
	 *
	 * @var string
	 */
	protected $_template;

	public function setTemplate($template)
	{
		$this->_template = (string) $template;
		return $this;
	}

	/**
	 * Set render object
	 *
	 * @param Zend_View_Interface $view Render object
	 *
	 * @return Tavs_Mail_Loader_File
	 */
	public function setView(Zend_View_Interface $view)
	{
		if ($view instanceof Zend_View_Interface) {
			$this->_view = $view;
		}
		else {
			throw new Tavs_Mail_Template_Exception('view must be instance of Zend_View_Interface');
		}
	}

	/**
	 * Return render object
	 *
	 * @return Zend_View_Interface
	 */
	public function getView()
	{
		if (null === $this->_view) {
			require_once 'Zend/Controller/Action/HelperBroker.php';
			$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
			$this->setView($viewRenderer->view);
		}

		return $this->_view;
	}

	/**
	 * Render script and return the output
	 *
	 * @return string
	 */
	protected function _render()
	{
		$view = $this->getView();
		$view->assign($this->_vars);

		try {
			return $view->render($this->_template);
		}
		catch (Zend_View_Exception $e) {
			throw new Tavs_Mail_Loader_Exception('unable to render ' . $this->_template.': ' . $e->getMessage());
		}
	}

}